package com.UnionPayCenter.distribution.manager.impl;

import com.UnionPayCenter.distribution.dto.PageQueryBaseDTO;
import com.UnionPayCenter.distribution.entity.DistributionSnDO;
import com.UnionPayCenter.distribution.manager.DistributionSnManager;
import com.UnionPayCenter.mapper.DistributionSnDOMapper;
import com.UnionPayCenter.service.ServiceTemplate;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Noah
 * @since 1.0.0, 2019/07/10
 */
@Service
public class DistributionSnManagerImpl implements DistributionSnManager {

    @Autowired
    private DistributionSnDOMapper distributionSnDOMapper;

    @Autowired
    private ServiceTemplate serviceTemplate;

    /**
     * 保存分销流水
     *
     * @param distributionSnDO 分销流水信息
     */
    @Override
    public void saveDistributionSn(DistributionSnDO distributionSnDO) {
        distributionSnDOMapper.insertSelective(distributionSnDO);
    }

    /**
     * 根据userid查询记录条数
     *
     * @param bId    商铺ID
     * @param shopId 店铺ID
     * @param userId 用户ID
     * @return
     */
    @Override
    public int countRecordByUserId(Long bId, Long shopId, Long userId, Integer level) {
        DistributionSnDO distributionSnDO = new DistributionSnDO();
        distributionSnDO.setBId(bId);
        distributionSnDO.setShareLvl(level);
        distributionSnDO.setShopId(shopId);
        distributionSnDO.setUserId(userId);
        return distributionSnDOMapper.countLevelRecordByUserId(distributionSnDO);
    }


    /**
     * 根据用户计算收益金额
     *
     * @param bId    商铺ID
     * @param shopId 店铺ID
     * @param userId 用户ID
     * @return
     */
    @Override
    public int sumShareAmountByUserId(Long bId, Long shopId, Long userId, Integer level) {
        DistributionSnDO distributionSnDO = new DistributionSnDO();
        distributionSnDO.setUserId(userId);
        distributionSnDO.setBId(bId);
        distributionSnDO.setShopId(shopId);
        distributionSnDO.setShareLvl(level);
        Integer rs = distributionSnDOMapper.sumShareAmountByUserId(distributionSnDO);
        return Objects.isNull(rs) ? 0 : rs;
    }


    /**
     * 根据用户信息获取分销流水
     *
     * @param bId              商铺ID
     * @param shopId           店铺ID
     * @param userId           用户ID
     * @param pageQueryBaseDTO
     * @param
     * @return 分销流水列表
     */
    @Override
    public Page<DistributionSnDO> listDistributionSn(Long bId, Long shopId, Long userId, Integer level, PageQueryBaseDTO pageQueryBaseDTO) {
        DistributionSnDO snDO = new DistributionSnDO();
        snDO.setBId(bId);
        snDO.setShopId(shopId);
        snDO.setUserId(userId);
        if (!new Integer(0).equals(snDO.getShareLvl())) {
            snDO.setShareLvl(level);
        }
        return serviceTemplate.pagination(pageQueryBaseDTO.getPage(), pageQueryBaseDTO.getPageSize(), () -> distributionSnDOMapper.listShareAmountByUserId(snDO));
    }


    /**
     * 根据用户信息获取分销流水
     *
     * @param bId    商铺ID
     * @param shopId 店铺ID
     * @param userId 用户ID
     * @return 分销流水列表
     */
    @Override
    public List<DistributionSnDO> listDistributionSn(Long bId, Long shopId, Long userId) {
        DistributionSnDO snDO = new DistributionSnDO();
        snDO.setBId(bId);
        snDO.setShopId(shopId);
        snDO.setUserId(userId);
        return distributionSnDOMapper.listShareAmountByUserId(snDO);
    }

    /**
     * 根据用户信息获取分销流水
     *
     * @param bId      商铺ID
     * @param shopId   店铺ID
     * @param userId   用户ID
     * @param page
     * @param pageSize
     * @return 分销流水列表
     */
    @Override
    public Page<DistributionSnDO> pageDistributionSn(Long bId, Long shopId, Long userId, Integer shareLvl, Integer page, Integer pageSize) {
        DistributionSnDO snDO = new DistributionSnDO();
        snDO.setBId(bId);
        snDO.setShopId(shopId);
        snDO.setUserId(userId);
        snDO.setShareLvl(shareLvl);
        return serviceTemplate.pagination(page, pageSize, () -> distributionSnDOMapper.listShareAmountByUserId(snDO));
    }
}
